package com.jinlei.crm.settings.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jinlei.crm.commons.exception.CustomException;
import com.jinlei.crm.commons.utils.UUIDUtil;
import com.jinlei.crm.settings.dao.DicValueDao;
import com.jinlei.crm.settings.entity.DicValue;
import com.jinlei.crm.settings.service.DicValueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DicValueServiceImpl implements DicValueService {

    @Autowired
    private DicValueDao dicValueDao;

    /*
        注入redisTemplate对象
     */
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public DicValue findById(String id) {
        return dicValueDao.selectById(id);
    }

    @Override
    public Map<String, Object> listPage(Integer pageNumber, Integer pageSize, DicValue dicValue) {
        PageHelper.startPage(pageNumber, pageSize);
        PageInfo<DicValue> pageInfo = new PageInfo<>(dicValueDao.select(dicValue));
        Map<String, Object> pageMap = new HashMap<>();
        pageMap.put("list", pageInfo.getList());
        pageMap.put("total", pageInfo.getTotal());
        return pageMap;
    }

    @Override
    public List<DicValue> list(DicValue dicValue) {
        // List<DicValue> list = null;
        // if(dicValue != null && StringUtils.hasLength(dicValue.getTypeCode())){
        //     // 判断redis中是否缓存数据
        //     list = (List<DicValue>) redisTemplate.opsForValue().get("sys:dicValue:" + dicValue.getTypeCode());
        //     // list 为空则没有缓存数据
        //     if(CollectionUtils.isEmpty(list)){
        //         // 查数据库
        //         list = dicValueDao.select(dicValue);
        //         // 将查询到的结果放在redis中
        //         redisTemplate.opsForValue().set("sys:dicValue:" + dicValue.getTypeCode(), list);
        //     }
        //     // 有缓存数据，直接返回缓存数据，不在查询数据库
        // }else{
        //     list = dicValueDao.select(dicValue);
        // }
        return dicValueDao.select(dicValue);
    }

    @Override
    public boolean insert(DicValue dicValue) {
        // 判断同一个字典类型编码下字典值value不能重复
        DicValue dbDicValue = dicValueDao.selectByType(dicValue.getTypeCode(),dicValue.getValue());
        if(dbDicValue != null){
            throw new CustomException(-20,"数据字典类型的字典值已存在");
        }
        dicValue.setId(UUIDUtil.getUUID());
        boolean result = dicValueDao.insert(dicValue) > 0;
        // 清除缓存
        redisTemplate.delete("sys:dicValue:" + dicValue.getTypeCode());
        return result;
    }

    @Override
    public boolean update(DicValue dicValue) {
        boolean result = dicValueDao.update(dicValue) > 0;
        // 清除缓存
        redisTemplate.delete("sys:dicValue:" + dicValue.getTypeCode());
        return result;
    }

    @Override
    public boolean remove(String id) {
        return dicValueDao.deleteById(id) > 0;
    }

    @Override
    public boolean removeBatch(String[] ids) {
        return dicValueDao.deleteBatch(ids) > 0;
    }
}
